import { Provider } from './vendors/weapp-redux.js';
import { store } from './redux/store.js';
import { API_wxLogin } from './redux/api.js'

let appConfig = {
  onLaunch: function() {
    wx.login({
      success: function(res) {
        const code = res.code
        wx.getUserInfo({
          success: function(res) {
            const { nickName, avatarUrl, gender, province, city } = res.userInfo
            const data = {
              code,
              gender,
              province,
              city,
              name: nickName,
              avatar: avatarUrl,
            }

            API_wxLogin(data)
            .then(res => {
              const { authorization, user } = res.data
              wx.setStorageSync('authorization', authorization)
              wx.setStorageSync('infoReady', user.infoReady)
              wx.setStorageSync('userId', user.id)
            })
          }
        })
      }
    })
  }
}

App(Provider(store)(appConfig))
